import pandas as pd
import numpy as np

def smaCal(tsprice, k):
    Sma = pd.Series(0.0, index=tsprice.index)
    for i in range(k-1, len(tsprice)):
        Sma[i] = sum(tsprice[(i-k+1):(i+1)]) / k
    return (Sma)

def wmaCal(tsprice,weight):
    k = len(weight)
    arrWeight = np.array(weight)
    Wma = pd.Series(0.0, index=tsprice.index)
    for i in range(k-1, len(tsprice.index)):
        Wma[i] = sum(arrWeight * tsprice[(i-k+1) : (i+1)])
    return (Wma)

def emaCal(tsprice,period=5, exponential=0.2):
    ewma = pd.Series(0.0, index=tsprice.index)
    ewma[period - 1] = np.mean(tsprice[:period])
    for i in range(period, len(tsprice)):
        ewma[i] = exponential * tsprice[i] + (1-exponential) * ewma[i - 1]
    return (ewma)

def ewmaCal(tsprice,period=5, exponential=0.2):
    ewma = pd.Series(0.0, index=tsprice.index)
    ewma[period - 1] = np.mean(tsprice[:period])
    for i in range(period, len(tsprice)):
        ewma[i] = exponential * tsprice[i] + \
                    (1-exponential) * ewma[i - 1]
    return (ewma)